package com.example.leetdemo.process;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

public class Solution473 {
    public boolean makesquare(int[] matchsticks) {
        int count = 0;
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < matchsticks.length; i++) {
            map.put(matchsticks[i], map.getOrDefault(matchsticks[i], 0) + 1);
            count += matchsticks[i];
        }
        if (count % 4 != 0 || count == 0) {
            return false;
        }
        int edge = count / 4;
        count = 0;
        if (map.containsKey(edge)) {
            count += map.get(edge);
        }
        map.remove(edge);
        for (int i = count; i <= 4; i++) {
            int edd = edge;
            while (edd > 0) {
                for (int j = edd; j >= 0; j --) {
                    if (map.containsKey(j)) {
                        int value = map.get(j);
                        if (value == 1) {
                            map.remove(j);
                        } else  {
                            map.put(j, value - 1);
                        }
                        edd -= j;
                        break;
                    }
                    if (j == 0) {
                        edd = -1;
                    }
                }
            }
            if (edd == 0) {
                count++;
            }
        }
        return count == 4;
    }

    public static void main(String[] args) {
        int minute = LocalDateTime.now().getMinute() + LocalDateTime.now().getHour() * 60;
        System.err.println(minute);
        Solution473 solution473 = new Solution473();
        System.err.println(solution473.makesquare(new int[] { 5,5,5,5,4,4,4,4,3,3,3,3}));
    }
}
